﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Buy : System.Web.UI.Page
{
    //生成订单

    MyDataDataContext db = new MyDataDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["GoodsID"] !=null)
        {

            //获取所购买商品的详细信息并展示在页面上
            string goodsID = Request.QueryString["GoodsID"];
            var goods = (from r in db.LookingOfGoods
                         where r.GoodsID == goodsID//Session["UserId"].ToString()//获取购买者信息
                         select r).First();
            goods.StateOfGoods = 0;//物品已经下单
            

            GoodsName.Text = goods.GoodsName;
            BuyerID.Text = Session["UserId"].ToString();//购买者ID
            var Buyer = (from r in db.User
                         where r.UserID == Session["UserId"].ToString()
                         select r).First();
            BuyerName.Text = Buyer.UserNickname;

            SaleID.Text =  goods.UserID;//出售者ID
            var Sale = (from r in db.User
                        where r.UserID == goods.UserID
                        select r).First();
            SaleName.Text = Sale.UserNickname;
            Money.Text = goods.PriceFloor.ToString();//显示价格下限
            string LsTime = DateTime.Now.ToString();//获取当下时间作为下单时间
            Time.Text = LsTime;


            //生成订单
            string str = System.Guid.NewGuid().ToString("N");
            orderID.Text = str;
            Order order = new Order
            {
                OrderID = str,
                SellerID = SaleID.Text,
                BuyerID = Session["UserId"].ToString(),
                GoodsID = goodsID,
                GoodsName = GoodsName.Text,
                Price = (float)Convert.ToSingle(Money.Text.Trim()),
                StateOfOrder = 1,//1表示订单处于进行中状态，0表示订单已完成, -1表示失败
                GoodsOrigin = 1,//1表示订单来源于SaleOfGoods, 0表示来源于LookingOfGoods
                Time = LsTime
            };
            db.Order.InsertOnSubmit(order);
        }    
    }

    protected void Back(object sender, EventArgs e)
    {
        Response.Redirect("Detail.aspx?GoodsID="+Request.QueryString["GoodsID"]);
    }

    protected void ChatingOnClick(object sender, EventArgs e)
    {
    }

    //确认订单
    protected void ComfirmOnClick(object sender, EventArgs e)
    {
        //保存插入记录
        db.SubmitChanges();
        //弹出订单创建成功提示
        Response.Write("<script language='javascript'>"); 
        Response.Write("alert('订单创建成功，请前往个人中心-我的订单-进行中查看。');"); 
        Response.Write("</script>"); 
    }

    //取消订单
    protected void CancelOnClick(object sender, EventArgs e)
    {
        string goodsID = Request.QueryString["GoodsID"];
        var goods = (from r in db.LookingOfGoods
                     where r.GoodsID == goodsID//Session["UserId"].ToString()//获取购买者信息
                     select r).First();
        goods.StateOfGoods = 1;//物品已经下单失败，继续发布
        //保存插入记录
        db.SubmitChanges();
        var order = (from r in db.Order
                     where r.SellerID == SaleID.Text & r.BuyerID == Session["UserID"].ToString() & r.OrderID == orderID.Text
                     select r).First();
        order.StateOfOrder = -1;
        //保存修改记录
        db.SubmitChanges();

        //弹出订单创建成功提示
        Response.Write("<script language='javascript'>");
        Response.Write("alert('订单取消成功，请前往个人中心-我的订单-失败订单查看。');");
        Response.Write("</script>");
    }
}